package com.jingdianjichi.auth.domain.service.impl;

import com.jingdianjichi.auth.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.auth.domain.entity.AuthRolePermissionBO;
import com.jingdianjichi.auth.domain.service.AuthRolePermissionDomainService;
import com.jingdianjichi.auth.infra.basic.entity.AuthRolePermission;
import com.jingdianjichi.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {

        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            //遍历authRolePermissionBO对象的权限id列表，对每个权限做操作
            AuthRolePermission authRolePermission = new AuthRolePermission();//创建一个新的对象
            authRolePermission.setRoleId(roleId);//将之前获取的角色ID设置到新创建的authRolePermission中
            authRolePermission.setPermissionId(permissionId);//权限id放里面
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());//设置未删除
            rolePermissionList.add(authRolePermission);//将新对象添加到列表中
        });
        //这里直接用一个批量插入，提高插入效率，因为一个一个插入要不断调用数据库，放一个列表里面一下子都插进去
        int count = authRolePermissionService.batchInsert(rolePermissionList);
        return count > 0;
    }


}
